<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Base Sprite</title>
</head>
<body>
  <canvas id="mycanvas" width="600" height="600"></canvas>
  <script src="/js/sprite-core.js"></script>
  <script>
    const BaseSprite = spritejs.BaseSprite,
      Group = spritejs.Group;
    const context = document.getElementById('mycanvas').getContext('2d');

    const s = new Group({
      anchor: 0.5,
      bgcolor: 'hsl(180,50%,50%)',
      size: [300, 300],
      pos: [300, 300],
    });
    s.connect(context).draw();
    s.on('afterdraw', (evt) => {
      // console.log(evt)
    });

    const s2 = new BaseSprite({
      anchor: 0.5,
      bgcolor: 'green',
      size: [50, 50],
      pos: [100, 100],
    });
    const animation = s2.animate([
      {pos: [100, 100]},
      {pos: [100, 300]},
      {pos: [300, 300]},
      {pos: [300, 100]},
      {pos: [100, 100]},
    ], {
      duration: 2000,
      iterations: Infinity,
    });
    s2.connect(context).draw();

    s.on('mouseenter', (evt) => {
      console.log(evt);
    });

    s.on('mouseleave', (evt) => {
      console.log(evt);
    });

    mycanvas.addEventListener('mousemove', (evt) => {
      const {offsetX, offsetY} = evt;
      s.dispatchEvent('mousemove', {layerX: offsetX, layerY: offsetY, originEvent: evt});
    });

    requestAnimationFrame(function f() {
      context.clearRect(0, 0, 600, 600);
      s.draw();
      s2.draw();
      // console.log(s2.attr())
      // s2.disconnect()
      requestAnimationFrame(f);
    });
  </script>
</body>
</html>